package com.jhgsys.internal.base.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.base.entity.BaseSafetyScoreConfig;
import com.jhgsys.internal.base.service.IBaseSafetyScoreConfigService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.UUID;

/**
 * 安全积分设置 Controller
 *
 * @author admin
 * @date 2024-08-07 11:34:15
 */
@Slf4j
@Validated
@Controller
public class BaseSafetyScoreConfigController extends BaseController {

    @Autowired
    private IBaseSafetyScoreConfigService baseSafetyScoreConfigService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseSafetyScoreConfig")
    @RequiresPermissions("baseSafetyScoreConfig:view")
    public String baseSafetyScoreConfigIndex(Model model){
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        model.addAttribute("tenantList",getTenantList());
        model.addAttribute("currentTenantId", getCurrentTenantId());
        if(StringUtils.isNotBlank(getCurrentTenantId())){
            BaseSafetyScoreConfig baseSafetyScoreConfig = baseSafetyScoreConfigService.findBaseSafetyScoreConfigByTenantId(getCurrentTenantId());
            if(baseSafetyScoreConfig!=null){
                model.addAttribute("baseSafetyScoreConfig",baseSafetyScoreConfig);
            }
        }
        return FebsUtil.view("base/baseSafetyScoreConfig/baseSafetyScoreConfigList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseSafetyScoreConfig/add")
    @RequiresPermissions("baseSafetyScoreConfig:add")
    public String baseSafetyScoreConfigAdd(){
        return FebsUtil.view("base/baseSafetyScoreConfig/baseSafetyScoreConfigEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseSafetyScoreConfig/update/{id}")
    @RequiresPermissions("baseSafetyScoreConfig:edit")
    public String baseSafetyScoreConfigUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("baseSafetyScoreConfig", baseSafetyScoreConfigService.findBaseSafetyScoreConfigById(id));
        return FebsUtil.view("base/baseSafetyScoreConfig/baseSafetyScoreConfigEdit");
    }

    /**
     * 查询（单个）
     * @param model
     * @param id
     * @return
     */
    @GetMapping("baseSafetyScoreConfig/search/{id}")
    @ResponseBody
    @RequiresPermissions("baseSafetyScoreConfig:view")
    public FebsResponse getBaseSafetyScoreConfigById(Model model, @PathVariable("id") String  id) {
        BaseSafetyScoreConfig baseSafetyScoreConfig = baseSafetyScoreConfigService.findBaseSafetyScoreConfigByTenantId(id);
        model.addAttribute("baseSafetyScoreConfig", baseSafetyScoreConfig);
        return new FebsResponse().success().data(model);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-07 11:34:15
     */
    @GetMapping("baseSafetyScoreConfig")
    @ResponseBody
    @RequiresPermissions("baseSafetyScoreConfig:view")
    public FebsResponse getAllBaseSafetyScoreConfigs(BaseSafetyScoreConfig baseSafetyScoreConfig) {
        return new FebsResponse().success().data(baseSafetyScoreConfigService.findBaseSafetyScoreConfigList(baseSafetyScoreConfig));
    }

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-07 11:34:15
     */
    @GetMapping("baseSafetyScoreConfig/list")
    @ResponseBody
    @RequiresPermissions("baseSafetyScoreConfig:view")
    public FebsResponse baseSafetyScoreConfigList(QueryRequest request, BaseSafetyScoreConfig baseSafetyScoreConfig) {
        Map<String, Object> dataTable = getDataTable(this.baseSafetyScoreConfigService.findBaseSafetyScoreConfigs(request, baseSafetyScoreConfig));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 BaseSafetyScoreConfig
     * @author admin
     * @date 2024-08-07 11:34:15
     */
    @Log("新增BaseSafetyScoreConfig")
    @PostMapping("baseSafetyScoreConfig")
    @ResponseBody
    @RequiresPermissions("baseSafetyScoreConfig:edit")
    public FebsResponse addBaseSafetyScoreConfig(@RequestBody BaseSafetyScoreConfig baseSafetyScoreConfig) throws FebsException {
        try {
            baseSafetyScoreConfig.setSafetyScoreConfigId(UUID.randomUUID().toString().replaceAll("-",""));
            baseSafetyScoreConfig.setCreatedBy(getCurrentUser().getUserName());
            this.baseSafetyScoreConfigService.createBaseSafetyScoreConfig(baseSafetyScoreConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增BaseSafetyScoreConfig失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 BaseSafetyScoreConfig
     * @author admin
     * @date 2024-08-07 11:34:15
     */
    @Log("逻辑删除BaseSafetyScoreConfig")
    @GetMapping("baseSafetyScoreConfig/delete/{id}")
    @ResponseBody
    @RequiresPermissions("baseSafetyScoreConfig:delete")
    public FebsResponse deleteBaseSafetyScoreConfig(@PathVariable("id") String baseSafetyScoreConfigIds) throws FebsException {
        try {
            BaseSafetyScoreConfig baseSafetyScoreConfig = new BaseSafetyScoreConfig();
            baseSafetyScoreConfig.setDeletedBy(getCurrentUser().getUserName());
            baseSafetyScoreConfig.setDeletedTime(new Date());
            baseSafetyScoreConfig.setDeletedFlg(FebsConstant.INVALID);
            this.baseSafetyScoreConfigService.deleteBaseSafetyScoreConfig(baseSafetyScoreConfig,baseSafetyScoreConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseSafetyScoreConfig失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 BaseSafetyScoreConfig
    * @author admin
    * @date 2024-08-07 11:34:15
    */
    @Log("物理删除BaseSafetyScoreConfig")
    @GetMapping("baseSafetyScoreConfig/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("baseSafetyScoreConfig:delete")
    public FebsResponse physicallyDeleteBaseSafetyScoreConfig(@PathVariable("id") String baseSafetyScoreConfigIds) throws FebsException {
        try {
            this.baseSafetyScoreConfigService.physicallyDeleteBaseSafetyScoreConfig(baseSafetyScoreConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseSafetyScoreConfig失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 BaseSafetyScoreConfig
     * @author admin
     * @date 2024-08-07 11:34:15
     */
    @Log("修改BaseSafetyScoreConfig")
    @PostMapping("baseSafetyScoreConfig/update")
    @ResponseBody
    @RequiresPermissions("baseSafetyScoreConfig:edit")
    public FebsResponse updateBaseSafetyScoreConfig(@RequestBody BaseSafetyScoreConfig baseSafetyScoreConfig) throws FebsException {
        try {
            baseSafetyScoreConfig.setUpdatedBy(getCurrentUser().getUserName());
            this.baseSafetyScoreConfigService.updateBaseSafetyScoreConfig(baseSafetyScoreConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改BaseSafetyScoreConfig失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 导出 BaseSafetyScoreConfig的excel
     * @author admin
     * @date 2024-08-07 11:34:15
     */
    @PostMapping("baseSafetyScoreConfig/excel")
    @ResponseBody
    @RequiresPermissions("baseSafetyScoreConfig:view")
    public void export(QueryRequest queryRequest, BaseSafetyScoreConfig baseSafetyScoreConfig, HttpServletResponse response) throws FebsException {
        try {
            List<BaseSafetyScoreConfig> baseSafetyScoreConfigs = this.baseSafetyScoreConfigService.findBaseSafetyScoreConfigs(queryRequest, baseSafetyScoreConfig).getRecords();
            ExcelKit.$Export(BaseSafetyScoreConfig.class, response).downXlsx(baseSafetyScoreConfigs, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
